package com.example.dailyrecord.entity;

import com.example.dailyrecord.util.FunctionUtils;
import com.example.dailyrecord.util.IDUtils;
import com.example.dailyrecord.util.PasswordEncoder;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@ApiModel("用户")
public class User {
    public static final int MAX_WRONG = 3;
    @ApiModelProperty("密码过期")
    private String credential;
    @ApiModelProperty("用户邮箱")
    private String email;
    @ApiModelProperty("用户可用时长")
    private String expired;
    @ApiModelProperty("")
    private String id;
    @ApiModelProperty("上次登录时间")
    private String lastLoginTime;
    @ApiModelProperty("用户名称")
    private String name;
    @ApiModelProperty("用户密码")
    private String password;
    @ApiModelProperty("注册时间")
    private String registerTime;
    @ApiModelProperty("密码错误次数，超过三次会锁定用户")
    private int wrongTimes;
    public static final String TABLE_NAME = "user";
    public static final String[] ALL_COLUMNS = {"`credential`", "`email`", "`expired`", "`id`", "`lastLoginTime`", "`name`", "`password`", "`registerTime`", "`wrongTimes`"};
    public static final String CREDENTIAL_COLUMN = "`credential`";
    public static final String CREDENTIAL_NAME = "credential";
    public static final String EMAIL_COLUMN = "`email`";
    public static final String EMAIL_NAME = "email";
    public static final String EXPIRED_COLUMN = "`expired`";
    public static final String EXPIRED_NAME = "expired";
    public static final String ID_COLUMN = "`id`";
    public static final String ID_NAME = "id";
    public static final String LASTLOGINTIME_COLUMN = "`lastLoginTime`";
    public static final String LASTLOGINTIME_NAME = "lastLoginTime";
    public static final String NAME_COLUMN = "`name`";
    public static final String NAME_NAME = "name";
    public static final String PASSWORD_COLUMN = "`password`";
    public static final String PASSWORD_NAME = "password";
    public static final String REGISTERTIME_COLUMN = "`registerTime`";
    public static final String REGISTERTIME_NAME = "registerTime";
    public static final String WRONGTIMES_COLUMN = "`wrongTimes`";
    public static final String WRONGTIMES_NAME = "wrongTimes";

    public boolean isAccountNonExpired() {
        return compareTime(expired);
    }

    public boolean isAccountNonLocked() {
        return wrongTimes < MAX_WRONG + 1;
    }

    public boolean isCredentialsNonExpired() {
        return compareTime(credential);
    }

    public boolean isEnabled() {
        return isAccountNonExpired() && isAccountNonLocked();
    }

    private static boolean compareTime(String early) {
        LocalDateTime earlyTime = FunctionUtils.parse(early);
        return earlyTime.compareTo(LocalDateTime.now()) > 0;
    }

    public static User init(String email, String password, String name) {
        User user = new User();
        user.setId(IDUtils.getStringID());
        user.setName(name);
        user.setPassword(PasswordEncoder.encoder(password));
        user.setEmail(email);
        user.setRegisterTime(FunctionUtils.getNowTime());
        user.setExpired(LocalDateTime.now().plusYears(1).format(FunctionUtils.YYYYMMDDHHMMSS));
        user.setCredential(LocalDateTime.now().plusMonths(1).format(FunctionUtils.YYYYMMDDHHMMSS));
        return user;
    }
}
